/*
 * Copyright (C) 2011 Mette Bank, Rikke Jensen, Kenneth Brodersen, Thomas Pedersen
 * 
 * This file is part of digiPECS.
 * 
 * digiPECS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * digiPECS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with digiPECS.  If not, see <http://www.gnu.org/licenses/>.
 */

package sw6.digipecs.admin;

import java.util.ArrayList;

import sw6.digipecs.R;
import sw6.digipecs.exceptions.ImageIsEmptyException;
import sw6.digipecs.model.ExtImage;
import sw6.digipecs.model.PECSBook;
import sw6.digipecs.widget.ExtImageAdapter;
import sw6.digipecs.widget.admin.HelpView;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ListAdapter;

/**
 * This class handles the activity responsible for modifying- and adding
 * pictures to the pecs book The activity ensures that pictures from the android
 * mediastore or the integrated camera can be added to the pecs book. Further
 * the activity supports the modification or deletion of current pictures in the
 * book.
 */
public class ManageImagesActivity extends Activity implements OnClickListener, OnItemClickListener {
	private Button mAddFromCameraButton;
	private Button mAddFromPhoneButton;
	private GridView mImageGrid;
	private PECSBook mBook;
	private Context mContext;
	private ArrayList<ExtImage> mImageList;
	private ListAdapter mAdapter;
	private HelpView mHelpView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.managepicturesmainlayout);
		initialise(); 
	}

	private void initialise() {
		mAddFromCameraButton = (Button) findViewById(R.id.addPictureFromCameraButton);
		mAddFromCameraButton.setOnClickListener(this); 
		mAddFromPhoneButton = (Button)findViewById(R.id.addPictureFromPhoneButton); 
		mAddFromPhoneButton.setOnClickListener(this); 
		mImageGrid = (GridView)findViewById(R.id.archiveGrid); 
		mImageGrid.setOnItemClickListener(this); 
		mContext = getApplicationContext();
		mHelpView = (HelpView)findViewById(R.id.helperManagePictures); 
		mHelpView.setHeadText("Håndter Arkiv"); 
		mHelpView.setBodyText("Tilpas et billedes egenskaber ved at klikke på det. Nye billeder kan tilføjes til arkivet med knapperne nederst på skærmen"); 
		
		updateList(); 
	}

	@Override
	public void onClick(View arg0) {
		Intent intent; 
		switch (arg0.getId()) {
		case R.id.addPictureFromCameraButton:
			intent = new Intent(this,AddFromCameraActivity.class); 
			startActivityForResult(intent, AddFromCameraActivity.REQUEST_CODE); 
			break;
		case R.id.addPictureFromPhoneButton:
			intent = new Intent(this,AddFromPhoneActivity.class); 
			startActivityForResult(intent, AddFromPhoneActivity.REQUEST_CODE); 
			break;
		}
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case AddFromCameraActivity.REQUEST_CODE:
			updateList(); 
			break;

		case EditPictureActivity.REQUEST_CODE:
			updateList(); 
			break;
		case AddFromPhoneActivity.REQUEST_CODE:
			updateList(); 
			break; 
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	private void updateList() {
		mBook = new PECSBook(mContext, PECSBook.Views.All); 
		mImageList = mBook.getAllPictures(); 
		mAdapter = new ExtImageAdapter(mContext,mImageList,new ExtImageAdapter.NonDraggableExtImageViewFactory()); 
		mImageGrid.setAdapter(mAdapter); 
		mBook.close(); 
	}
	@Override
	protected void onResume() {
		initialise(); 
		super.onResume();
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		ExtImage selectedImage = mImageList.get(arg2); 
		int imageId=0; 
		try {
			imageId = selectedImage.getId();
		} catch (ImageIsEmptyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		Intent intent = new Intent(this,EditPictureActivity.class); 
		intent.putExtra(EditPictureActivity.EXTRA_PICTUREID, imageId); 
		startActivityForResult(intent, EditPictureActivity.REQUEST_CODE); 
	}
}